#include"lcd.h"


/*液晶屏的操作*/

// 初始化液晶屏
void lcd_init(LCD *lcd)
{
	// 打开液晶屏
	lcd->fd = open("/dev/fb0", O_RDWR);
    if(lcd->fd == -1)
    {
        perror("打开 /dev/fb0 失败");
        exit(0);
    }
	
	// 获取屏幕可变信息
	struct fb_var_screeninfo varinfo; // 可变属性
	if(ioctl(lcd->fd, FBIOGET_VSCREENINFO, &varinfo) != 0)
    {
        perror("获取LCD设备可变属性信息失败");
        return;
    }
	// 获取屏幕的基本信息
	lcd->width = varinfo.xres;
	lcd->height = varinfo.yres;
	lcd->bpp = varinfo.bits_per_pixel;
	
	
	lcd->row_size = lcd->width * lcd->bpp / 8;
	lcd->size = lcd->row_size * lcd->height;
	
	// 申请一块两倍与屏幕的映射内存
    lcd->fbmem = mmap(NULL, lcd->size,
                PROT_READ|PROT_WRITE,
                MAP_SHARED, lcd->fd, 0); 
	if(lcd->fbmem == MAP_FAILED){
		perror("映射 LCD 失败");
		close(lcd->fd);
        exit(0);
	}
}

/*反初始化液晶屏*/
void lcd_uninit(LCD *lcd)
{
	munmap(lcd->fbmem, lcd->size);
	close(lcd->fd);
}

